package com.yasaka.stock.common;

import com.yasaka.stock.exception.MyException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * <DL>
 * <DT><B> 类名称:文件处理工具类 </B></DT>
 * <p>
 * <DT><B> 类说明: 对磁盘文件进行属性或物理操作 </B></DT>
 * </DL>
 * <p>
 * @author Daniel
 * @version 1.00, 2014-7-28
 */
@Component
@Slf4j
public class FileUtils {

    /**
     * 传入文件流并保存文件
     *
     * @param stream      文件流
     * @param filename    文件名
     * @throws MyException
     */
	public static void saveFileFromInputStream(InputStream stream,String filename) throws MyException{
        FileOutputStream fs = null;
	    try{
            fs = new FileOutputStream(filename);
            byte[] buffer =new byte[1024*1024];
            int byteread = 0;
            while ((byteread=stream.read(buffer))!=-1)
            {
                fs.write(buffer,0,byteread);
                fs.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            throw new MyException(e.getMessage());
        } finally {
            try {
                fs.close();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw new MyException(e.getMessage());
            }
            try {
                stream.close();
            } catch (IOException e) {
                log.error(e.getMessage(), e);
                throw new MyException(e.getMessage());
            }
        }
	}


    /**
     * <br>
     * <b>方法名称: 磁盘文件名处理</b><br>
     * <b>方法说明: 根据传入的文件名构造磁盘文件名称(15位日期yyyymmddhhmmsss+.后缀名)</b><br>
     * <br>
     * @param srcDiskName 源文件名
     * @return
     * @throws MyException
     */
    public static String diskNameProductor(String srcDiskName) throws MyException {
        try {
            String fileType = fileTypeProductor(srcDiskName);
            String prefixFileName = DateUtil.getYyyyMMddHHmm() + String.valueOf((int)(Math.random()*900)+100) ;
            StringBuffer str = new StringBuffer();
            str.append(prefixFileName);
            str.append(AmzxConstant.FILE_TYPE_PRIFIX);
            str.append(fileType);
            return str.toString();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            throw new MyException(e.getMessage());
        }
    }


    /**
     * <br>
     * <b>方法名称: 文件类型处理</b><br>
     * <b>方法说明: 根据传入的文件名构造类型名</b><br>
     * <br>
     * @param srcDiskName 源文件名
     * @return
     * @throws MyException
     */
    public static String fileTypeProductor(String srcDiskName) throws MyException {
        try {
            int i = StringUtils.lastIndexOf(srcDiskName, AmzxConstant.FILE_TYPE_PRIFIX) + 1;
            int j = srcDiskName.length();
            String fileType = StringUtils.substring(srcDiskName, i,j);
            return fileType.toLowerCase();
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
            throw new MyException(e.getMessage());
        }
    }
}
